user nginx;
worker_processes auto;

error_log /dev/stderr warn;
pid        /tmp/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format json_combined escape=json '{ "time_local": "$time_local", '
        '"host": "$host", '
        '"remote_addr": "$remote_addr", '
        '"remote_user": "$remote_user", '
        '"request": "$request", '
        '"status": "$status", '
        '"body_bytes_sent": "$body_bytes_sent", '
        '"request_time": "$request_time", '
        '"http_referrer": "$http_referer", '
        '"http_user_agent": "$http_user_agent" }';

    keepalive_timeout  65;
    port_in_redirect off;
    gzip off;

    map $host $lang {
        hostnames;
        "deckhouse.ru"    "ru";
        "*.deckhouse.ru"  "ru";
        # for test environments
        "deckhouse.ru.*"  "ru";
        "ru.localhost"    "ru";
        default           "en";
    }

    server {
        listen       80;
        server_name  _;

        root   /app;
        index  index.html;
        error_page 403 404 /$lang/404.html;

        charset utf-8;
        access_log     /dev/stdout json_combined;

        location = /healthz {
            access_log  off;
            add_header Content-Type text/plain;
            return 200;
        }

        location /link_test_report.txt {
          deny all;
        }

        location ~* ^/documentation/(v[\d]+[^\/]*|latest)/(presentations|assets|images|js|css)/(.+)$ {
            try_files /$2/$3 /$2/$3/ =404;
        }

        location ~* ^/documentation/(v[\d]+[^\/]*|latest)/compare(/(ru|en))?/(.+)$ {
            try_files /compare/$4 /compare/$4/ =404;
        }

        location ~* ^/documentation/(v[\d]+[^\/]*|latest)/(search-index.json)$ {
            try_files /$2 =404;
        }

        rewrite ^/(.+)/099-ceph-csi$ /$1/031-ceph-csi/ permanent;
        rewrite ^/(.+)/010-priority-class$ /$1/001-priority-class/ permanent;
        rewrite ^/(.+)/010-priority-class(/.*)$ /$1/001-priority-class$2 permanent;
        rewrite ^/(.+)/020-deckhouse$ /$1/002-deckhouse/$2 permanent;
        rewrite ^/(.+)/020-deckhouse(/.*)$ /$1/002-deckhouse$2 permanent;
        rewrite ^/(.+)/810-deckhouse-web$ /$1/810-documentation/$2 permanent;
        rewrite ^/(.+)/810-deckhouse-web(/.*)$ /$1/810-documentation$2 permanent;

        location ~* ^/documentation/(v[\d]+[^\/]*|latest)/(.+)?$ {
            try_files /$lang/$2 /$lang/$2/index.html /$lang/$2/ =404;
        }

        location / {
            try_files $uri $uri/ =404;
        }
    }

}
